package com.yulongtian.interviewWorks.day20220823;

/**
 * @author YulongTian
 * @date 2022/8/23 - 14:35
 */
public class InterviewsWork02 {
    public static void main(String[] args) {
        System.out.println(CheckPermutation("abc", "bca"));
    }

    ////不能解决含有重复字母的情况
    //public static boolean CheckPermutation(String s1, String s2) {
    //    int res1 = 0;
    //    int res2 = 0;
    //    for (int i = 0; i < s1.length(); i++) {
    //        res1 |= 1 << (s1.charAt(i) - 'a');
    //    }
    //    for (int i = 0; i < s2.length(); i++) {
    //        res2 |= 1 << (s2.charAt(i) - 'a');
    //    }
    //    System.out.println(Integer.toBinaryString(res1));
    //    System.out.println(Integer.toBinaryString(res2));
    //    return (res1 ^ res2) == (Math.abs(res1 - res2));
    //}

    public static boolean CheckPermutation(String s1, String s2) {
        if(s1.length()!=s2.length()){
            return false;
        }
        int[] arr1 = new int[26];
        int[] arr2 = new int[26];

        for (int i = 0; i < s1.length(); i++) {
            arr1[s1.charAt(i) - 'a']++;
        }
        for (int i = 0; i < s2.length(); i++) {
            arr2[s2.charAt(i) - 'a']++;
        }

        boolean flag1 = false;
        boolean flag2 = false;

        for (int i = 0; i < 26; i++) {
            if (arr1[i] == arr2[i]) {
                continue;
            }
            if (arr1[i] < arr2[i]) {
                flag1 = true;
            }
            if (arr1[i] > arr2[i]) {
                flag2 = true;
            }
            if (flag1 && flag2) {
                return false;
            }

        }
        return true;

    }

}
